<?php
namespace Cyfy\Modules\DibiPager\Forms;
use \Cyfy\Modules\Forms;

/**
 * Creates a new menu
 */
class SortBy extends Forms\Form
{
    /**
     * How To Sort
     */
    public $options = array();
    
    /**
     * Selecting ASC/DESC
     */
    public $allowAscDesc = true;
    
    /**
     * Allow onpage number
     */
    public $allowOnPage = true; 
    
    /**
     * Onpage by default
     */ 
    public $onPage = 10;
     
    /**
     * Structure
     */
	function structure()
    {
        
        $i = new Forms\Select("sort-by", self :: t(100), $this -> options, $_SESSION["sort-by"][$this->pagerName]);
        $this -> addItem($i);
        
        if($this -> allowAscDesc)
        {
            $options = array(
                'ASC' => self :: t(120),
                'DESC' => self :: t(130)
            );
            $i = new Forms\Select("sort-how", self :: t(110), $options, $_SESSION["sort-how"][$this->pagerName]);
            $this -> addItem($i);
        }
        
        if($this -> allowAscDesc)
        {
            $i = new Forms\TextBox("onPage", self :: t(140));
            $i -> format = 'number';
            $i -> maxValue = 100;
            $i -> minValue = 2;
            $this -> addItem($i);
        }
        
		$b = new Forms\Button(self :: t(150));
		$this -> addButton($b);
    }
	
    function submit()
    {
        $pagerName = $this->getTemp("pagerName");
        
        
        // if onpage changed - must reload whole page
        if($_SESSION["pagerOnPage"][$pagerName] != $this -> get("onPage"))
        {
            // reload blocks and after show message
            $msg = \Cyfy\Message :: get(self :: t(160));
            $this -> onSubmit = "Blocks.reload('Cyfy', 'content', '', 'Cyfy.setMessages(\'{$msg}\')');";
            \Cyfy\Message :: set(self :: t(170), \Cyfy\Message::WARNING);
        }
        // reloading just the table
        else
        {
            \Cyfy\Message :: set(self :: t(160));
            $this -> onSubmit = $this->getTemp("onSubmit");
        }

        // new values
        $_SESSION["sort-by"][$pagerName] = $this -> get("sort-by");
        $_SESSION["sort-how"][$pagerName] = $this -> get("sort-how");
        $_SESSION["pagerOnPage"][$pagerName] = $this -> get("onPage");

        
        
    }
    
}
